package com.Dao.impl;

import com.Dao.StudentDao;
import com.domain.Student;

import java.sql.*;
import java.util.ArrayList;

public class StudentDaoimpl02 implements StudentDao {
    @Override
    public ArrayList<Student> findAll() throws SQLException {
        ArrayList<Student> students = new ArrayList<>();
        Connection connection;
        Statement statement;
        ResultSet resultSet;
        try {
            connection = JDBCUtilis.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from student");
            while (resultSet.next()) {
                int id = resultSet.getInt("sid");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String birthday = resultSet.getString("birthday");
                Student student = new Student((Integer) id, name, age, birthday);
                students.add(student);
            }
            JDBCUtilis.Close(resultSet,statement,connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return students;
    }

    @Override
    public Student findById(Integer id) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        Student student=null;
        try {
            connection = JDBCUtilis.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from student where sid="+(int)id);
            while (resultSet.next()){
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String birthday = resultSet.getString("birthday");
                student = new Student((Integer) id,name,age,birthday);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        JDBCUtilis.Close(resultSet,statement,connection);
        return student;
    }

    @Override
    public int insert(Student stu) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        int i=0;
        try {
            connection = JDBCUtilis.getConnection();
            String sql = "INSERT INTO student VALUES (?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,stu.getSid());
            preparedStatement.setString(2,stu.getName());
            preparedStatement.setInt(3,stu.getAge());
            preparedStatement.setString(4,stu.getBirthday());
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        JDBCUtilis.Close(resultSet,statement,connection);
        return i;
    }

    @Override
    public int update(Student stu) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        int i =0;
        try {
            connection = JDBCUtilis.getConnection();
            String sql = "update student set name=? where sid=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,stu.getName());
            preparedStatement.setInt(2,stu.getSid());
            i = statement.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        JDBCUtilis.Close(resultSet,statement,connection);
        return i;
    }

    @Override
    public int delete(Integer id) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        int i =0;
        try {
            connection = JDBCUtilis.getConnection();
            statement = connection.createStatement();
            String sql = "delete from student where sid="+id;
            i = statement.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        JDBCUtilis.Close(resultSet,statement,connection);
        return i;
    }
}
